
//
//  XLStatusBottomView.swift
//  SinaProjectTest
//
//  Created by zhanghaifeng on 16/10/27.
//  Copyright © 2016年 BBSWaitting. All rights reserved.
//

import UIKit

class XLStatusBottomView: UIView {

    // 设置btn 属性以便给btn的title赋值
    var retweetBtn: UIButton?
    var commentBtn: UIButton?
    var unlikeBtn: UIButton?
    
    // 设置一个属性以便外界赋值
    var bottomViewModel: XLHomeViewModel?{
        didSet{
        
            retweetBtn?.setTitle(bottomViewModel?.reposts_countStr, for: UIControlState.normal)
            commentBtn?.setTitle(bottomViewModel?.comments_countStr, for: UIControlState.normal)
            unlikeBtn?.setTitle(bottomViewModel?.attitudes_countStr, for: UIControlState.normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 监听事件
    @objc private func buttonClock(){
        
    }
    // 创建视图
    private func setupUI(){
        backgroundColor = XLRandomColor()
        // 添加控件
        retweetBtn = addChildBtn(imageName: "timeline_icon_retweet", title: "转发")
        commentBtn = addChildBtn(imageName: "timeline_icon_comment", title: "评论")
        unlikeBtn = addChildBtn(imageName: "timeline_icon_unlike", title: "赞")
        let leftLineImage = addChildLines()
        let rightLineImage = addChildLines()
        // 设置约束
        retweetBtn!.snp_makeConstraints { (make) in
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(commentBtn!)
        }
        
        commentBtn!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(retweetBtn!.snp_right)
            make.width.equalTo(unlikeBtn!)
        }
        
        unlikeBtn!.snp_makeConstraints { (make) in
            make.top.right.bottom.equalTo(self)
            make.left.equalTo(commentBtn!.snp_right)
        }
        
        leftLineImage.snp_makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetBtn!.snp_right)
        }
        
        rightLineImage.snp_makeConstraints { (make) in
            make.centerY.equalTo(leftLineImage)
            make.centerX.equalTo(commentBtn!.snp_right)
        }
    }
    
    // 添加按钮的公共方法
    func addChildBtn(imageName: String, title: String) -> UIButton{
        let button = UIButton(setHighLightImageNamed: imageName, title: title, target: self, action: #selector(buttonClock))
        
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), for: UIControlState.highlighted)
        
//        button.setBackgroundImage(UIImage(named: "timeline_retweet_background"), for: UIControlState.normal)
//        button.setBackgroundImage(UIImage(named: "timeline_retweet_background_highlighted"), for: UIControlState.highlighted)
//        
        // 添加button
        addSubview(button)
        return button
    }
    // 添加分割线的公共方法
    func addChildLines() -> UIImageView{
        let imageLine = UIImageView(imgNamed: "timeline_card_bottom_line")
        // 添加控件
        addSubview(imageLine)
        return imageLine
    }
}
